package com.iflysse.controller;


import com.iflysse.common.ActivityState;
import com.iflysse.common.ResultEnum;
import com.iflysse.pojo.bo.CommonBoResp;
import com.iflysse.pojo.bo.req.*;
import com.iflysse.pojo.bo.resp.StudentTaskDetailAnalyseBoResp;
import com.iflysse.pojo.bo.resp.StudentTestDetailAnalyseBoResp;
import com.iflysse.pojo.bo.resp.TeachingActivityBoResp;
import com.iflysse.pojo.bo.resp.TeachingActivityListBoResp;
import com.iflysse.pojo.vo.common.CommonVoResp;
import com.iflysse.pojo.vo.req.ActivityIdVoReq;
import com.iflysse.pojo.vo.req.ActivityStateVoReq;
import com.iflysse.pojo.vo.req.AddTestVoReq;
import com.iflysse.pojo.vo.req.TeachingClassIdVoReq;
import com.iflysse.pojo.vo.resp.*;
import com.iflysse.services.ActivityService;
import com.iflysse.services.TaskService;
import com.iflysse.services.TestService;
import com.iflysse.util.CommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/classActivity")
@Api(value = "老师端教学活动")
@Log4j
public class ClassActivityController{

    @Autowired
    private ActivityService activityService;
    @Autowired
    private TestService testService;
    @Autowired
    private TaskService taskService;



    /**
     * 教师端获取学生学习任务详情
     */
    @PostMapping("/queryStudentTaskDetail")
    @ApiOperation("教师端获取学生任务学习详情")
    public StudentTaskDetailAnalyseVoResp queryStudentTaskDetail(ActivityIdVoReq activityIdVoReq){
        StudentTaskDetailAnalyseVoResp studentTaskDetailAnalyseVoResp=new StudentTaskDetailAnalyseVoResp();
        try{
            log.info("教师端获取学生学习任务详情参数bean:"+activityIdVoReq);
            TaskIdBoReq taskIdBoReq= CommonUtil.objToObj(activityIdVoReq,TaskIdBoReq.class);
            taskIdBoReq.setTaskId(activityIdVoReq.getActivityId());
            StudentTaskDetailAnalyseBoResp studentTaskDetailAnalyseBoResp= taskService.getStudentTaskDetailAnalyseByTestId(taskIdBoReq);
            log.info("教师端获取学生学习任务taskId:"+taskIdBoReq.getTaskId()+"详情任务响应bean:"+studentTaskDetailAnalyseBoResp);
            StudentTaskDetailAnalyseResp result= CommonUtil.objToObj(studentTaskDetailAnalyseBoResp,StudentTaskDetailAnalyseResp.class);
            result.setTaskExpirationDate(CommonUtil.dateSourceToString(studentTaskDetailAnalyseBoResp.getTaskExpirationDate(),CommonUtil.pattern));
            result.setUpdateTime(CommonUtil.dateSourceToString(studentTaskDetailAnalyseBoResp.getUpdateTime(),CommonUtil.pattern));

            if(CommonUtil.stringSourceToDate(result.getTaskExpirationDate(),CommonUtil.pattern).getTime()>new Date().getTime()){
                Long time=CommonUtil.stringSourceToDate(result.getTaskExpirationDate(),CommonUtil.pattern).getTime()-new Date().getTime();

                result.setRemainingTime(time.intValue()/1000/60);

            }else {
                result.setRemainingTime(0);

            }
            studentTaskDetailAnalyseVoResp.setResult(result);
            studentTaskDetailAnalyseVoResp.setCode(ResultEnum.SUCCESS.getCode());
            studentTaskDetailAnalyseVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("教师端获取学生学习任务taskId:"+activityIdVoReq.getActivityId()+"的响应bean:"+studentTaskDetailAnalyseVoResp);
            return studentTaskDetailAnalyseVoResp;
        }catch (Exception e){
            log.error("教师端获取学生学习任务出现异常："+e);
            CommonUtil.printError(studentTaskDetailAnalyseVoResp);
            return studentTaskDetailAnalyseVoResp;
        }


    }




    /**
     * 教师端获取学生测试详情
     */
    @PostMapping("/queryStudentTestDetail")
    @ApiOperation("教师端获取学生测试详情")
    public StudentTestDetailAnalyseVoResp queryStudentTestDetail(ActivityIdVoReq activityIdVoReq){

        StudentTestDetailAnalyseVoResp studentTestDetailAnalyseVoResp=new StudentTestDetailAnalyseVoResp();
        try{

            log.info("教师端获取学生测试详情参数bean:"+activityIdVoReq);
            TestIdBoReq testIdBoReq=CommonUtil.objToObj(activityIdVoReq,TestIdBoReq.class);
            testIdBoReq.setTestId(activityIdVoReq.getActivityId());
            StudentTestDetailAnalyseBoResp studentTestDetailAnalyseBoResp= testService.getStudentTestDetailAnalyseByTestId(testIdBoReq);
            StudentTestDetailAnalyseResp studentTestDetailAnalyseResp=CommonUtil.objToObj(studentTestDetailAnalyseBoResp,StudentTestDetailAnalyseResp.class);

            studentTestDetailAnalyseResp.setTestId(studentTestDetailAnalyseBoResp.getTestId());
            studentTestDetailAnalyseResp.setTestTitle(studentTestDetailAnalyseBoResp.getTestTitle());
            studentTestDetailAnalyseResp.setTeacherName(studentTestDetailAnalyseBoResp.getTeacherName());
            studentTestDetailAnalyseResp.setPublishDate(CommonUtil.dateSourceToString(studentTestDetailAnalyseBoResp.getPublishDate(),CommonUtil.pattern));
            studentTestDetailAnalyseResp.setExpirationDate(CommonUtil.dateSourceToString(studentTestDetailAnalyseBoResp.getExpirationDate(),CommonUtil.pattern));
            studentTestDetailAnalyseResp.setTestState(studentTestDetailAnalyseBoResp.getTestState());
            studentTestDetailAnalyseResp.setNumberOfComplete(studentTestDetailAnalyseBoResp.getNumberOfComplete());
            studentTestDetailAnalyseResp.setStudentTestState(studentTestDetailAnalyseBoResp.getStudentTestState());
            studentTestDetailAnalyseVoResp.setResult(studentTestDetailAnalyseResp);
            studentTestDetailAnalyseVoResp.setCode(ResultEnum.SUCCESS.getCode());
            studentTestDetailAnalyseVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("教师端获取学生测试详情响应bean:"+studentTestDetailAnalyseVoResp);

            return studentTestDetailAnalyseVoResp;
        }catch (Exception e){
            log.error("教师端获取学生测试详情出现异常："+e);
            CommonUtil.printError(studentTestDetailAnalyseVoResp);
            return studentTestDetailAnalyseVoResp;
        }

    }







    /**
     * 老师端 查询教学班级内得教学活动列表
     *
     */
    @PostMapping("/loadActivityByTeacherClassId")
    @ApiOperation("老师端查询教学班级内得教学活动列表")
   public ClassActivityVoResp doLoadActivityByTeacherClassId(TeachingClassIdVoReq teachingClassIdVoReq){
        ClassActivityVoResp classActivityVoResp=new ClassActivityVoResp();
        try{
            log.info("老师端查询教学班级内得教学活动列表参数bean:"+teachingClassIdVoReq);
            TeachingClassIdBoReq teachingClassIdBoReq= CommonUtil.objToObj(teachingClassIdVoReq, TeachingClassIdBoReq.class);
            log.info("老师端查询教学班级内得教学活动列表调用业务接口参数："+teachingClassIdBoReq);
            TeachingActivityListBoResp teachingActivityListBoResp= activityService.loadTeachiActivityListByTeachingClassId(teachingClassIdBoReq);
            log.info("老师端查询教学班级内得教学活动列表调用业务接口响应bean："+teachingActivityListBoResp);
            List<TeachingActivityBoResp> teachingActivityList =teachingActivityListBoResp.getTeachingActivityList();
            List<ClassActivityResp> result=new ArrayList<ClassActivityResp>();
            teachingActivityList.forEach(teachingActivityBoResp -> {
                ClassActivityResp classActivityResp=CommonUtil.objToObj(teachingActivityBoResp,ClassActivityResp.class);
                classActivityResp.setExpirationDate(CommonUtil.dateSourceToString(teachingActivityBoResp.getExpirationDate(),CommonUtil.pattern));
                classActivityResp.setUpdateTime(CommonUtil.dateSourceToString(teachingActivityBoResp.getUpdateTime(),CommonUtil.pattern));
                classActivityResp.setState(teachingActivityBoResp.getState().getStateId());
                result.add(classActivityResp);
            });
            classActivityVoResp.setResult(result);
            classActivityVoResp.setCode(ResultEnum.SUCCESS.getCode());
            classActivityVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
           return classActivityVoResp;

        }catch (Exception e){
            log.error("老师端查询教学班级内得教学活动列表出现异常："+e);
            CommonUtil.printError(classActivityVoResp);
            return classActivityVoResp;
        }
   }


    /**
     * 老师端 发布测试
     */
    @PostMapping("/addTest")
    @ApiOperation("老师端发布测试")
   public CommonVoResp doAddTest(AddTestVoReq addTestVoReq){

        CommonVoResp commonVoResp=new CommonVoResp();
        try{

            log.info("老师端发布测试参数bean："+addTestVoReq);
            TestBoReq testBoReq=  CommonUtil.objToObj(addTestVoReq, TestBoReq.class);
            testBoReq.setExpirationDate(CommonUtil.stringSourceToDate(addTestVoReq.getExpirationDate(),CommonUtil.pattern));
            testBoReq.setQuestionIdList(Arrays.asList(addTestVoReq.getQuestionIdList().split(",")));
            if(addTestVoReq.getState().equals(ActivityState.STATE_NO_PUBLISH.getStateId())){
                testBoReq.setState(ActivityState.STATE_NO_PUBLISH);
            }else if(addTestVoReq.getState().equals(ActivityState.STATE_FINISH.getStateId())){
                testBoReq.setState(ActivityState.STATE_FINISH);
            }else if(addTestVoReq.getState().equals(ActivityState.STATE_RUNNING.getStateId())){
                testBoReq.setState(ActivityState.STATE_RUNNING);
            }

            log.info("老师端发布测试调用业务接口参数bean:"+testBoReq);
            CommonBoResp commonBoResp= activityService.saveOrPublishTest(testBoReq);

            CommonUtil.printBoResultToVoResult(commonBoResp,commonVoResp);
            log.info("老师端发布测试响应bean:"+commonVoResp);

            return commonVoResp;
        }catch (Exception e){
            log.error("老师端发布测试出现异常："+e);
            CommonUtil.printError(commonVoResp);
            return commonVoResp;
        }
   }


    /**
     * 老师端教学班级中 删除活动
     */
    @PostMapping("/deleteActivity")
    @ApiOperation("老师端教学班级中删除活动")
   public CommonVoResp doDeleteActivity(ActivityIdVoReq activityIdVoReq){
        CommonVoResp commonVoResp=new CommonVoResp();
        try{

            ActivityIdBoReq activityIdBoReq= CommonUtil.objToObj(activityIdVoReq, ActivityIdBoReq.class);
            log.info("老师端教学班级中删除活动调用业务接口参数bean:"+activityIdBoReq);
            CommonBoResp commonBoResp=  activityService.deleteActivityByActivityId(activityIdBoReq);
            log.info("老师端教学班级中删除活动响应bean:"+commonBoResp);
            CommonUtil.printBoResultToVoResult(commonBoResp,commonVoResp);

            return commonVoResp;
        }catch (Exception e){
            log.error("老师端教学班级中删除活动出现异常："+e);
            CommonUtil.printError(commonVoResp);
            return commonVoResp;
        }
   }


    /**
     * 教师端 教学活动 更新活动状态,包括暂存后点击发布功能
     */
    @PostMapping("/updateActivityState")
    @ApiOperation("教师端教学活动更新活动状态,包括暂存后点击发布功能")
   public CommonVoResp doUpdateActivityState(ActivityStateVoReq activityStateVoReq) {

        CommonVoResp commonVoResp=new CommonVoResp();
        try{

            log.info("教师端教学活动更新活动状态参数bean："+activityStateVoReq);
            ActivityStateBoReq activityStateBoReq=CommonUtil.objToObj(activityStateVoReq,ActivityStateBoReq.class);

            if(activityStateVoReq.getState().equals(ActivityState.STATE_NO_PUBLISH.getStateId())){
                activityStateBoReq.setState(ActivityState.STATE_NO_PUBLISH);
            }else if(activityStateVoReq.getState().equals(ActivityState.STATE_FINISH.getStateId())){
                activityStateBoReq.setState(ActivityState.STATE_FINISH);
            }else if(activityStateVoReq.getState().equals(ActivityState.STATE_RUNNING.getStateId())){
                activityStateBoReq.setState(ActivityState.STATE_RUNNING);
            }

//           activityStateBoReq.getState().setStateId(activityStateVoReq.getState());
            log.info("教师端教学活动更新活动状态参数bean:"+activityStateBoReq);
            CommonBoResp commonBoResp= activityService.updateActivityState(activityStateBoReq);
            log.info("教师端教学活动更新活动状态响应bean:"+commonBoResp);
            CommonUtil.printBoResultToVoResult(commonBoResp,commonVoResp);

            return commonVoResp;
        }catch (Exception e){
            log.error("教师端教学活动更新活动状态出现异常："+e);
            CommonUtil.printError(commonVoResp);
            return commonVoResp;
        }
    }
}
